﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Services;
using OpenPhotoAlbum.Controls;
using OpenPhotoAlbum.DbAccess;

namespace OpenPhotoAlbum.Shared
{
	/// <summary>
	/// Summary description for Comments
	/// </summary>
	[WebService(Namespace = "http://tempuri.org/")]
	[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
	[System.ComponentModel.ToolboxItem(false)]
	// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
	[System.Web.Script.Services.ScriptService]
	public class Comments : AlbumWebService
	{
		public const string WRONG_CAPTCHA_VALUE = "Wrong captcha value.";

		[WebMethod]
		public string GetComments(string photoId)
		{
			TimePointDescriber describer = new TimePointDescriber(
				Resources.Strings.TimePoint_Format_YearsAgo,
				Resources.Strings.TimePoint_Format_YearAgo,
				Resources.Strings.TimePoint_Format_MonthsAgo,
				Resources.Strings.TimePoint_Format_MonthAgo,
				Resources.Strings.TimePoint_Format_WeeksAgo,
				Resources.Strings.TimePoint_Format_WeekAgo,
				Resources.Strings.TimePoint_Format_DaysAgo,
				Resources.Strings.TimePoint_Format_DayAgo,
				Resources.Strings.TimePoint_Format_HoursAgo,
				Resources.Strings.TimePoint_Format_HourAgo,
				Resources.Strings.TimePoint_Format_MinutesAgo,
				Resources.Strings.TimePoint_Format_JustNow);

			Guid id = new Guid(photoId);
			IEnumerable<Comment> comments = BusinessLogics.CommentManager.Instance.GetComments(id);
			List<CommentDisplayItem> displayItemList = new List<CommentDisplayItem>();
			foreach (Comment comment in comments)
			{
				CommentDisplayItem item = new CommentDisplayItem();
				item.Id = comment.CommentId.ToString();
				item.Icon = UrlContext.BuildPhotoPipeUserIconUrl(comment.UserId);
				if (comment.User == null)
				{
					item.Name = Resources.Strings.Comments_AnonymousUser;
					item.Link = string.Empty;
				}
				else
				{
					item.Name = comment.User.UserName;
					item.Link = UrlContext.BuildSharedHomeUrl(comment.User.UserName);
				}
				item.Comment = HttpUtility.HtmlEncode(comment.CommentContent);
				item.Comment = item.Comment.Replace(Environment.NewLine, "<BR />");
				item.Time = describer.Describe(comment.CommentDateTime);
				displayItemList.Add(item);
			}
			JavaScriptSerializer jss = new JavaScriptSerializer();
			return jss.Serialize(displayItemList);
		}

		[WebMethod]
		public void SubmitComment(string photoId, string content, string captchaValue, string captchaData)
		{
			int n;

			try { n = int.Parse(captchaValue); }
			catch (FormatException) { throw new Exception(WRONG_CAPTCHA_VALUE); }
			
			if (CaptchaDataServices.Instance.Encrypt(n) != captchaData)
			{
				throw new Exception(WRONG_CAPTCHA_VALUE);
			}

			string name = HttpContext.Current.User.Identity.Name;
			if (!string.IsNullOrEmpty(name))
			{
				DbAccess.User user = BusinessLogics.UserManager.Instance.GetUser(name);
				Guid userId = user.UserId;
				BusinessLogics.CommentManager.Instance.AddComment(
					userId, new Guid(photoId), DateTime.UtcNow, HttpContext.Current.Request.UserHostAddress, content);
			}
			else
			{
				BusinessLogics.CommentManager.Instance.AddComment(
					null, new Guid(photoId), DateTime.UtcNow, HttpContext.Current.Request.UserHostAddress, content);
			}
		}
	}

	public class CommentDisplayItem
	{
		public string Id { get; set; }
		public string Icon { get; set; }
		public string Name { get; set; }
		public string Link { get; set; }
		public string Comment { get; set; }
		public string Time { get; set; }
	}
}
